import { Component } from '@angular/core';
import {BorrowList} from "../../shared/model/borrowList.interface";
import {BorrowApiService} from "../../core/api/borrow-api.service";
import {Router} from "@angular/router";
import {ConfirmationService, Message} from "primeng/api";
import {UserInfoService} from "../../serve/userinfo";
import {BorrowListTemp} from "../../serve/borrowListTemp";

@Component({
  selector: 'app-borrow',
  templateUrl: './borrow.component.html',
  styleUrls: ['./borrow.component.css']
})
export class BorrowComponent {
  msgs: Message[] = [{summary: "borrows", severity: "info"}]
    borrowLists : BorrowList[]=[];
    constructor(private readonly _borrowApiService: BorrowApiService,
                private readonly _router: Router,
                private readonly _confirmationService: ConfirmationService,
                private readonly _userInfo :UserInfoService,
                private readonly _AllBorrowTemp:BorrowListTemp) {
    }
  ngOnInit(): void {
    this._borrowApiService.count().subscribe(({count}) => {
      this.msgs = [{summary: `${count} borrow`, severity: "info"}];
    })

    this._borrowApiService.getAll(this._userInfo.user.id).subscribe((borrowLists) => {
      console.log(borrowLists)
      this._AllBorrowTemp.AllborrowList=borrowLists
      this.borrowLists = borrowLists;
    })
  }
  delete(BorrowId: number, event: any) {
    this._confirmationService.confirm({
      target: event.target,
      message: 'Are you sure that you want to return book?',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        //confirm action
        this._borrowApiService.delete(BorrowId).subscribe({
          next: () => {
            // reload user list
            this.ngOnInit();
          },
          error: () => {
            this.msgs = [{summary: `Delete Failed!`, severity: "error"}];
          }
        })
      },
      reject: () => {
        //reject action
        console.log("master branch")
      }
    });
  }


}
